package com.audible.library.domain.beans;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * This is a lightweight persistence domain object representing user_audio_book table in the database.
 *
 * @author Jigar Shah
 * @version 0.1
 */
@Entity
@Table(name = "user_audio_book")
@NamedQueries({
    @NamedQuery(name = "UserAudioBook.findAll", query = "SELECT u FROM UserAudioBook u"),
    @NamedQuery(name = "UserAudioBook.findByUserProfileId", query = "SELECT u FROM UserAudioBook u WHERE u.userAudioBookPK.userProfileId = :userProfileId"),
    @NamedQuery(name = "UserAudioBook.findByAudioBookId", query = "SELECT u FROM UserAudioBook u WHERE u.userAudioBookPK.audioBookId = :audioBookId"),
    @NamedQuery(name = "UserAudioBook.findByPurchaseDate", query = "SELECT u FROM UserAudioBook u WHERE u.purchaseDate = :purchaseDate"),
    @NamedQuery(name = "UserAudioBook.findByDownloaded", query = "SELECT u FROM UserAudioBook u WHERE u.downloaded = :downloaded")})
public class UserAudioBook implements Serializable {

    private static final long serialVersionUID = -7000895474197110596L;
    
    @EmbeddedId
    protected UserAudioBookPK userAudioBookPK;

    @Basic(optional = false)
    @Column(name = "PURCHASE_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date purchaseDate;

    @Column(name = "DOWNLOADED")
    private String downloaded;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "userAudioBook")
    private UserAudioBookRating userAudioBookRating;

    @JoinColumn(name = "AUDIO_BOOK_ID", referencedColumnName = "ID", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private AudioBook audioBook;

    @JoinColumn(name = "USER_PROFILE_ID", referencedColumnName = "ID", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private UserProfile userProfile;

    public UserAudioBook() {
    }

    public UserAudioBook(UserAudioBookPK userAudioBookPK) {
        this.userAudioBookPK = userAudioBookPK;
    }

    public UserAudioBook(UserAudioBookPK userAudioBookPK, Date purchaseDate) {
        this.userAudioBookPK = userAudioBookPK;
        this.purchaseDate = purchaseDate;
    }

    public UserAudioBook(int userProfileId, int audioBookId) {
        this.userAudioBookPK = new UserAudioBookPK(userProfileId, audioBookId);
    }

    public UserAudioBookPK getUserAudioBookPK() {
        return userAudioBookPK;
    }

    public void setUserAudioBookPK(UserAudioBookPK userAudioBookPK) {
        this.userAudioBookPK = userAudioBookPK;
    }

    public Date getPurchaseDate() {
        return purchaseDate;
    }

    public void setPurchaseDate(Date purchaseDate) {
        this.purchaseDate = purchaseDate;
    }

    public String getDownloaded() {
        return downloaded;
    }

    public void setDownloaded(String downloaded) {
        this.downloaded = downloaded;
    }

    public UserAudioBookRating getUserAudioBookRating() {
        return userAudioBookRating;
    }

    public void setUserAudioBookRating(UserAudioBookRating userAudioBookRating) {
        this.userAudioBookRating = userAudioBookRating;
    }

    public AudioBook getAudioBook() {
        return audioBook;
    }

    public void setAudioBook(AudioBook audioBook) {
        this.audioBook = audioBook;
    }

    public UserProfile getUserProfile() {
        return userProfile;
    }

    public void setUserProfile(UserProfile userProfile) {
        this.userProfile = userProfile;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (userAudioBookPK != null ? userAudioBookPK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof UserAudioBook)) {
            return false;
        }
        UserAudioBook other = (UserAudioBook) object;
        if ((this.userAudioBookPK == null && other.userAudioBookPK != null) || (this.userAudioBookPK != null && !this.userAudioBookPK.equals(other.userAudioBookPK))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.audible.library.domain.beans.UserAudioBook[userAudioBookPK=" + userAudioBookPK + "]";
    }

}
